package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Account;
import com.sl.au.entity.EXDELoginInfo;
import com.sl.au.entity.Grading;
import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.Role;
import com.sl.au.entity.Template;
import com.sl.au.entity.User;
import com.sl.au.repository.AccountRepository;
import com.sl.au.repository.EXDELoginInfoRepository;
import com.sl.au.repository.GridUserDetailsRepository;
import com.sl.au.repository.TemplateDetailsRepository;
import com.sl.au.repository.TemplateRepository;
import com.sl.au.repository.UserRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.GridUserDetailsService;
import com.sl.au.service.ModuleService;
import com.sl.au.service.TemplateDetailsService;
import com.sl.au.service.TemplateService;

@Service("gridUserDetailsService")
@Transactional
public class GridUserDetailsServiceImpl extends BaseServiceImpl implements GridUserDetailsService {

	@Autowired
	GridUserDetailsRepository gridUserDetailsRepository;
	
	@Autowired
	UserRepository userRepository;
	
	@Override
	public GridUserDetailsRepository getRepository() {
		return this.gridUserDetailsRepository;
	}
	
	@Override
	public List<User> getNotSetUserPageByKey(int start, int length, String gridId, String key,
			String orderName, String dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)) {
			pages = userRepository.getGridUserDetailsNotSetUserPageByKey(gridId, key, pageable);
		}else {
			pages = userRepository.getGridUserDetailsNotSetUserPage(gridId, pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getNotSetUserPageByKeyCount(String gridId, String key) {
		int count = 0;
		Pageable pageable = new PageRequest( 0, Integer.MAX_VALUE);
		if(StringUtils.hasText(key)) {
			count = userRepository.getGridUserDetailsNotSetUserPageByKey(gridId, key, pageable).getContent().size();
		}else {
			count = userRepository.getGridUserDetailsNotSetUserPage(gridId, pageable).getContent().size();
		}
		return count;
	}

	@Override
	public List<User> getSetUserPageByKey(int start, int length, String templateId, String gradingId, String key,
			String orderName, String dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)) {
			pages = userRepository.getGridUserDetailsSetUserPageByKey(templateId, gradingId, key, pageable);
		}else {
			pages = userRepository.getGridUserDetailsSetUserPage(templateId, gradingId, pageable);
		}
		return pages.getContent();
	}
	
	@Override
	public int getSetUserPageByKeyCount(String templateId, String gradingId, String key) {
		int count = 0;
		Pageable pageable = new PageRequest( 0, Integer.MAX_VALUE);
		if(StringUtils.hasText(key)) {
			count = userRepository.getGridUserDetailsSetUserPageByKey(templateId, gradingId, key, pageable).getContent().size();
		}else {
			count = userRepository.getGridUserDetailsSetUserPage(templateId, gradingId, pageable).getContent().size();
		}
		return count;
	}

}
